<template>
  <div class="sale_all">
    <!-- 面包屑 -->
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item>销售员管理</el-breadcrumb-item>
      <el-breadcrumb-item>销售员列表</el-breadcrumb-item>
    </el-breadcrumb>
    <!-- 销售员列表 -->
    <template>
      <el-table :data="tableData" style="width: 100%">
        <el-table-column label="id" width="250" align="center">
          <template slot-scope="scope">
            <span style="margin-left: 10px">{{ scope.row._id }}</span>
          </template>
        </el-table-column>
        <el-table-column label="姓名" width="120" align="center">
          <template slot-scope="scope">
            <el-popover trigger="hover" placement="top">
              <p>姓名: {{ scope.row.salesName }}</p>
              <div slot="reference" class="name-wrapper">
                <el-tag size="medium">{{ scope.row.salesName }}</el-tag>
              </div>
            </el-popover>
          </template>
        </el-table-column>
        <el-table-column label="年龄" width="80" align="center">
          <template slot-scope="scope">
            <span style="margin-left: 10px">{{ scope.row.age }}</span>
          </template>
        </el-table-column>
        <el-table-column label="性别" width="80" align="center">
          <template slot-scope="scope">
            <span style="margin-left: 10px">{{ scope.row.gender }}</span>
          </template>
        </el-table-column>
        <el-table-column label="头像" width="80" align="center">
          <el-avatar src="https://cube.elemecdn.com/0/88/03b0d39583f48206768a7534e55bcpng.png"></el-avatar>
        </el-table-column>
        <el-table-column label="客户" width="80" align="center">
          <template slot-scope="scope">
            <el-button size="mini" type="primary" @click="handleEdit2(scope.row._id)">查看</el-button>
          </template>
        </el-table-column>
        <el-table-column label="操作" align="center">
          <template slot-scope="scope">
            <el-button size="mini" type="success" @click="handleEdit(scope.row)">修改</el-button>
            <el-button size="mini" type="danger" @click="handleDelete(scope.row._id)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
    </template>
    <!-- 修改信息 -->
    <template>
      <el-drawer
        :append-to-body="true"
        :before-close="handleClose"
        :visible.sync="drawer"
        size="50%"
        align="center"
      >
        <div class="form-size" style="width:600px">
          <el-form
            :model="ruleForm"
            status-icon
            :rules="rules"
            ref="ruleForm"
            label-width="140px"
            class="demo-ruleForm"
          >
            <h2 style="margin-bottom:30px">修改销售员信息</h2>
            <el-form-item label="新账户名:" prop="newsalesName">
              <el-input v-model.lazy="ruleForm.newsalesName" label-width="200px"></el-input>
            </el-form-item>
            <el-form-item label="新密码:" prop="newsalesPassword">
              <el-input type="password" v-model.lazy="ruleForm.newsalesPassword" autocomplete="off"></el-input>
            </el-form-item>
            <el-form-item label="新年龄:" prop="age">
              <el-input v-model.lazy="ruleForm.age" autocomplete="off"></el-input>
            </el-form-item>
            <el-form-item label="性别:">
              <el-radio v-model="ruleForm.gender" label="男">男</el-radio>
              <el-radio v-model="ruleForm.gender" label="女">女</el-radio>
            </el-form-item>
            <el-form-item label="头像:">
              <el-avatar src="https://cube.elemecdn.com/0/88/03b0d39583f48206768a7534e55bcpng.png"></el-avatar>
            </el-form-item>
            <el-form-item label="销售员权限:">
              <el-radio v-model="state_radio" label="common">启用</el-radio>
              <el-radio v-model="state_radio" label="down">禁用</el-radio>
            </el-form-item>
            <el-form-item>
              <el-button type="primary" @click="submitForm('ruleForm')">确定</el-button>
              <el-button @click="resetForm('ruleForm')">重置</el-button>
            </el-form-item>
          </el-form>
        </div>
      </el-drawer>
    </template>
  </div>
</template>

<script>
import { createNamespacedHelpers } from "vuex";
const { mapState, mapMutations, mapActions } = createNamespacedHelpers(
  "salesperson"
);
export default {
  data() {
    //修改验证
    var checkName = (rule, value, callback) => {
      if (!value) {
        return callback(new Error("用户名不能为空"));
      }
      callback();
    };
    var checkAge = (rule, value, callback) => {
      if (!value) {
        return callback(new Error("年龄不能为空"));
      }
      callback();
    };
    var validatePass = (rule, value, callback) => {
      if (value === "") {
        callback(new Error("请输入密码"));
      }
      callback();
    };
    return {
      drawer: false,
      rules: {
        newsalesPassword: [{ validator: validatePass, trigger: "blur" }],
        newsalesName: [{ validator: checkName, trigger: "blur" }],
        age: [
          { validator: checkAge, message: "请输入年龄", trigger: "blur" },
          {
            min: 1,
            max: 3,
            message: "长度在 1 到 3 个字符",
            trigger: "blur",
          },
        ],
      },
      state_radio: "common",
    };
  },
  computed: {
    ...mapState(["tableData", "ruleForm"]),
  },
  created() {
    this.getSalesPerson();
  },
  methods: {
    //获取销售人员
    ...mapActions([
      "getSalesPerson",
      "deleteSalesPerson",
      "updataSalesPerson",
      "getCustomer",
    ]),
    ...mapMutations(["setDrawer", "setInnerDrawer", "set_id", "reset"]),
    //打开编辑
    handleEdit(info) {
      this.drawer = !this.drawer;
      this.set_id(info);
    },
    //删除
    async handleDelete(_id) {
      const data = await this.deleteSalesPerson(_id);
      if (data.status) {
        this.getSalesPerson();
      } else {
        alert(data.message);
      }
    },
    //编辑确定
    handleClose(done) {
      this.$confirm("你确定要离开吗？")
        .then((_) => {
          done();
        })
        .catch((_) => {});
    },
    //新信息验证
    async submitForm(formName) {
      const data = await this.updataSalesPerson(this.state_radio);
      this.$refs[formName].validate((valid) => {
        if (valid) {
          if (data.status) {
            alert("修改成功");
            this.getSalesPerson();
          }
        }
      });
    },
    //重置
    resetForm(formName) {
      this.reset();
    },
    //查看客户
    async handleEdit2(_id) {
      const data = await this.getCustomer(_id);
      if (data.status) {
        this.$router.push("/system/CheckCustomer");
      }
    },
  },
};
</script>

<style lang="scss" scoped>
@import "./css/salesperson.scss";
</style>